package com.kadron.service;

import java.io.Serializable;
import java.util.Date;

import org.jboss.seam.Component;
import org.jboss.seam.annotations.AutoCreate;
import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;

import com.kadron.common.KConst;
import com.kadron.common.KInitParams;
import com.kadron.common.KUtil;
import com.kadron.dao.KUserDAO;
import com.kadron.dao.KUserKadronsDAO;
import com.kadron.entity.User;
import com.kadron.entity.UserKadrons;
import com.kadron.enumeration.UserType;

@AutoCreate
@Name("userService")
public class KUserService implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7095865941873497453L;
	
	@In
	private KUserDAO userDAO;
	@In
	private UserHistoryService userHistoryService;
	
	public User updateStatus(User user) {
		synchronized (user) {	
			UserType userType = UserType.valueOf(user.getUserType().toString());
			switch (userType) {
			case STANDARD_FIST_BUY:
			case STANDARD_10:
				int discount = UserDiscountService.getDiscount(user);
				if (discount <= 0) {
					user.setUserType(UserType.STANDARD);
				}
				user.setStatusUpdate(KUtil.getMoment());			
				userHistoryService.create(user, KConst.USER_TYPE_UPDATE, KConst.USER_TYPE_UPDATE, "User type was updated after new buy. OldType="+userType+", NewType="+user.getUserType()+"!");
				KUserDAO userDAO = (KUserDAO) Component.getInstance("userDAO");
				user = (User) userDAO.update(user);
				break;
				
			default:
				break;
			}			
		}
		return user;
	}	
	
	public User checkStatus(long userId) {
		KUserKadronsDAO userKadronsDAO = (KUserKadronsDAO) Component.getInstance("userKadronsDAO");
		User user = userDAO.find(userId);
		synchronized (user) {	
			UserType oldUserType = UserType.valueOf(user.getUserType().toString());
			UserType newUserType = null;
			
			Long userKadronsFromLastUpdate = userKadronsDAO.getKadronsFromLastUpdate(userId);
			
			Date moment = KUtil.getMoment();
			Date lastUpdate = user.getStatusUpdate();
			Date yearAgo = KUtil.getYearAgo(moment);
			Date monthAgo = KUtil.getMonthAgo(moment);
			
			int kadronsFromLastUpdate = 0;
			if (userKadronsFromLastUpdate != null) {
				kadronsFromLastUpdate = userKadronsFromLastUpdate.intValue();
			}
			
			if ((lastUpdate.before(yearAgo) || lastUpdate.equals(yearAgo)) 
					&& (UserType.SILVER.equals(oldUserType)
						|| UserType.GOLD.equals(oldUserType)
						|| UserType.PLATINUM.equals(oldUserType))
					) {				
				if (kadronsFromLastUpdate >= KInitParams.SILVER_KADRONS && kadronsFromLastUpdate < KInitParams.GOLD_KADRONS) {
					newUserType = UserType.SILVER;
					
				} else if (kadronsFromLastUpdate >= KInitParams.GOLD_KADRONS && kadronsFromLastUpdate < KInitParams.PLATINUM_KADRONS) {
					newUserType = UserType.GOLD;
					
				} else if (kadronsFromLastUpdate >= KInitParams.PLATINUM_KADRONS) {
					newUserType = UserType.PLATINUM;
					
				} else {
					newUserType = UserType.STANDARD;				
				}
			} else if ((lastUpdate.before(monthAgo) || lastUpdate.equals(monthAgo)) 
					&& UserType.STANDARD_10.equals(oldUserType)) {
				newUserType = UserType.STANDARD;								
			}
			userHistoryService.create(user, KConst.USER_TYPE_UPDATE, KConst.USER_TYPE_UPDATE, "User type was updated. OldType="+oldUserType+", NewType="+newUserType+"! User kadrons from last update = "+ kadronsFromLastUpdate);
			user.setUserType(newUserType);
			user.setStatusUpdate(moment);
			user = (User) userDAO.update(user);
		}
		return user;
	}	
	
	public User updateStatus(User user, long userKadrons,long orderKadrons) {
		
		synchronized (user) {			
			Date moment = KUtil.getMoment();
			Long finalKadrons = orderKadrons + userKadrons;
			UserType oldType = UserType.valueOf(user.getUserType().toString());
			
			if (finalKadrons >= KInitParams.PLATINUM_KADRONS && !oldType.equals(UserType.PLATINUM)) {
				user.setUserType(UserType.PLATINUM);
			} else if (finalKadrons >= KInitParams.GOLD_KADRONS && !oldType.equals(UserType.GOLD)) {
				user.setUserType(UserType.GOLD);
			} else if (finalKadrons >= KInitParams.SILVER_KADRONS && !oldType.equals(UserType.SILVER)) {
				user.setUserType(UserType.SILVER);					
			}
			if (!oldType.equals(user.getUserType())) {
				user.setStatusUpdate(moment);			
				userHistoryService.create(user, KConst.USER_TYPE_UPDATE, KConst.USER_TYPE_UPDATE, "User type was updated after new buy. OldType="+oldType+", NewType="+user.getUserType()+"!");
				user = (User) userDAO.update(user);
			}
			return user; 
		}
	}
	
	public void createUserKadrons(User user, long kadrons, String text) {
		KUserKadronsDAO userKadronsDAO = (KUserKadronsDAO) Component.getInstance("userKadronsDAO");
		UserKadrons userKadrons = new UserKadrons(user, kadrons, text);
		userKadronsDAO.update(userKadrons);
		userHistoryService.create(user, KConst.KADRONS, KConst.KADRONS, "Stored user kadrons="+kadrons+" in DB");
	}
	
	public UserType getUserType(User user) {
		if (user == null) {
			throw new NullPointerException();
		}
		KUserDAO userDAO = (KUserDAO) Component.getInstance("userDAO");
		synchronized (user) {			
			return userDAO.getUserType(user.getId());
		}
	}
}
